package com.sky.mapper;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);

    void insertBatch(List<OrderDetail> orderDetailList);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    List<OrderVO> getOrderList(OrdersPageQueryDTO dto);

    //    =================================================day09实战
    @Update("update Orders set status=#{status} where id=#{id}")
    void updateStatus(OrdersConfirmDTO dto);


    @Select("select o.*,u.name user_name from orders o,user u  where o.id=#{id} and o.user_id=u.id; ")
    Orders getOrderById(Integer id);


    @Select("select * from order_detail WHERE order_id=#{id}")
    List<OrderDetail> getOrderDetailById(Integer id);


    @Update("update Orders set status=6,cancel_reason=#{cancelReason} where id=#{id}")
    void updateCancelOrdersStatus(OrdersCancelDTO dto);

    @Select("select status from orders where id=#{id}")
    Orders getStatusOfOrder(Long id);

    @Update("update Orders set status=6,rejection_reason=#{rejectionReason} where id=#{id}")
    void updateRejectionOrder(OrdersRejectionDTO dto);

    @Select("select count(1) from orders where status=#{status}")
    Integer getStatusNum(Integer status);

    List<OrderVO> getBysTatusAndUserId(Integer status, Long userId);

    @Select("select * from orders where status=#{status} and order_time<#{time} ")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);


    Double getTurnoverByDay(LocalDateTime beginTime, LocalDateTime endTime, Integer status);

    Integer getOrderByTime(LocalDateTime beginTime, LocalDateTime endTime,Integer status);

    @MapKey("id")
    List<Map> getTop10ByTime(LocalDateTime beginTime, LocalDateTime endTime);


    Integer countByMap(Map map);
}
